package cn.donting.jme.edit.ui.scenetree.meum.geometry;

import cn.donting.jme.edit.EditorState;
import cn.donting.jme.edit.ui.scenetree.ClipBoard;
import cn.donting.jme.edit.ui.scenetree.SceneTreeNodeView;
import cn.donting.jme.edit.ui.scenetree.meum.AbsTreeMenuItem;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class BoxMenuItem extends AbsTreeMenuItem {

    public BoxMenuItem() {
        super("立方体           ");
    }

    /**
     * 距离摄像机多远
     */
    private int dis=10;
    @Override
    public boolean isAvailable(ClipBoard clipBoard) {
        Spatial selectSpatial = clipBoard.getSelectSpatial();
        if (selectSpatial instanceof Node) {
            return true;
        }
        return false;
    }

    @Override
    public void action(ClipBoard clipBoard) {
        Spatial selectSpatial = clipBoard.getSelectSpatial();
        if (selectSpatial instanceof Node) {
            Camera camera = EditorState.getJmeEditSceneApp().getCamera();
            Vector3f pos = camera.getDirection().mult(dis);

            Geometry geometry = new Geometry("Box", new Box(1, 1, 1));
            geometry.setMaterial(EditorState.getJmeEditSceneApp().loadMaterial("Common/MatDefs/Light/Lighting.j3md"));
            Vector3f worldToLocal = selectSpatial.worldToLocal(pos, new Vector3f());
            geometry.setLocalTranslation(worldToLocal);
            //把 geometry 加入到场景
            SceneTreeNodeView.getInstance().addSpatialToSelectNode(geometry);
        }
    }
}
